<template>
    <div
        class="contract-detial-page revoke-collection-detial"
        :class="readOnly ? 'read':'write'"
    >
        <Card>
            <Affix :offset-top="120">
                <div class="contract-detial-affix">
                    <div style="text-align: left;"
                         v-if="!this.isSP && this.params.editFlag && this.params.processStatus!=='draft' && this.params.processStatus!=='toBeRepaired'">
                        <Button type="primary" @click="back()">返回</Button>
                        <Button type="primary" v-if="!readOnly" @click="through">通过</Button>
                        <Button type="primary" v-if="!readOnly" @click="refuse">拒绝</Button>
                    </div>
                    <div style="text-align: right;" v-else>
                        <Button type="primary" @click="back()">返回</Button>
                        <!--       <Button type="primary" v-if="!readOnly" @click="save()">保存</Button>-->
                        <Button type="primary" v-if="!readOnly" @click="submit">提交</Button>
                    </div>
                </div>
            </Affix>
            <Form
                ref="contractForm"
                :model="contractForm"
                :rules="contractFormValidate"
                :label-width="100"
            >
                <Collapse v-model="openPanel">
                    <Panel name="0" v-if="!this.isSP && this.params.editFlag">
                        <!-- <Alert>日志信息</Alert> -->
                        日志信息
                        <div class="content" slot="content">
                            <approveHistory :Obj="Obj"/>
                        </div>
                    </Panel>
                    <Panel name="1">
                        <!-- <Alert>基本信息</Alert> -->
                        基本信息
                        <div class="content" slot="content">
                            <BaseInfo ref="baseInfo" :formData="pageInfo"/>
                        </div>
                    </Panel>
                    <Panel name="2">
                        <!-- <Alert>合同信息</Alert> -->
                        合同信息
                        <div class="content" slot="content">
                            <contractInfo
                                :Obj="Obj"
                                :readOnly="readOnly"
                                ref="contractInfo"
                            />
                        </div>
                    </Panel>

                    <Panel name="4">
                        <!-- <Alert>金融产品</Alert> -->
                        金融产品
                        <div class="content" slot="content">
                            <FinanceInfo ref="financeInfo" :Obj="Obj"/>
                        </div>
                    </Panel>
                    <Panel name="5">
                        收据信息
                        <div class="content" slot="content" v-if="show">
                            <RevokeFrom
                                ref="RevokeFrom"
                                :formData="revokeFrom"
                                :Obj="Obj"
                                :dataDict="dataDict"
                            />
                        </div>
                    </Panel>
                    <Panel name="6" v-show="false">
                      业务办理
                        <div class="content" slot="content">
                            <Business ref="business" :Obj="Obj" :fileParams="fileParams" :isUploadFileModalOpen="isUploadFileModalOpen" @openUploadModal="openUploadModal" @closeModal="closeModal" :readOnly="readOnly"/>
                        </div>
                    </Panel>
                    <Panel name="7"
                           v-if="!readOnly && !this.isSP && this.params.editFlag && this.params.processStatus!=='draft' && this.params.processStatus!=='toBeRepaired'">
                        <!-- <Alert>当前用户: {{userInfo ? userInfo.userRealName :''}}</Alert> -->
                        当前用户: {{userInfo ? userInfo.userRealName :''}}
                        <div class="content" slot="content">
                            <ContractCurrentUser ref="contractCurrentUser" @fnThrough="fnThrough" :needFile=needFile
                                                 :Obj="{type:'collectionRevoke'} " />
                        </div>
                    </Panel>
                </Collapse>
            </Form>
        </Card>
        <Spin size="large" fix v-if="spinShow"></Spin>
    </div>
</template>
<script>
    import {mapGetters} from "vuex";
    import {businessSource, channelType, FinanceType} from "_p/basic/api/contract/channel-type/channel-type";
    import {getDictDataList} from "_p/basic/assets/contract/js/utils";
    import {findByContractNo} from "_p/basic/api/contract/activated-case/index";
    // 收据信息
    import RevokeFrom from "../RevokeFrom";
    // 业务办理
    import Business from "./Business";
    // 基本信息
    import BaseInfo from "_p/basic/pages/contract/common/base-info";
    // 用户信息
    import CustomerInfo from "_p/basic/pages/contract/common/customer-info";
    // 金融产品
    import FinanceInfo from "_p/basic/pages/contract/common/finance-info/finance-info";
    // 合同信息
    import contractInfo from "_p/basic/pages/contract/common/contract-info/contract-info";
    // 日志信息
    import approveHistory from "_p/basic/pages/contract/common/approve-history/approve-history";
    // 当前用户
    import ContractCurrentUser from "_p/basic/pages/contract/common/current-user/contractCurrentUser";
    import {
        revokedStartProcess, getManageCollectCancel, getManageCollectCancelByCaseNo,
        revokeSubmitProcess, saveCollectCancel, agreeValid
    } from "_p/basic/api/contract/revoke/revoke-collection";
    import {checkFile} from "_p/basic/api/image/image-upload";

    export default {
        // 合同收款撤销
        name: "revokeCollectionDetial",
        components: {
            RevokeFrom,
            BaseInfo,
            Business,
            FinanceInfo,
            contractInfo,
            CustomerInfo,
            approveHistory,
            ContractCurrentUser
        },
        data() {
            return {
                flag: true,
                overpaymentFlag: false,
                spinShow: false,
                type: null,
                needFile: false,
                params: {},
                revokeFrom: {},
                fileParams: {},
                isUploadFileModalOpen: false,
                readOnly: null,
                contractBusiness: ["collectionRevoke"],
                pageParams: {},
                contractNo: null,
                creditStatus: null,
                paymentStatus: null,
                show: false,
                customerInfo: {},
                Obj: {},
                caseNo: null,
                id: null,
                loading: true,
                defaultContractData: {},
                contractForm: {},
                contractFormValidate: {},
                pageInfo: {},
                // 数据字典
                dicttArr: [
                    "bankCode", "receiptSourceType", "receiptStatus", "feeType"
                ],
                dataDict: {},
                modalType: "",
                modalTitle: "",
                modal: false,
                openPanel: ["1", "2", "4", "5", "6", "7"]
            };
        },
        computed: {
            ...mapGetters({userInfo: "userInfo", roleType: "roles"}),
            // 是否是合作商
            isSP: function () {
                return channelType(this.roleType)
            },
            isFinance: function () {
                return FinanceType(this.roleType)
            },
        },
        created() {
            this.getParams();
        },
        mounted() {
            this.getDictDataList();
            if (this.params.editFlag) {
                this.getManageCollectCancel();
            } else {
                this.revokeFrom = this.params;
                this.show = true;
            }
        },

        methods: {
            // 获取初始数据
            getParams() {
                const params = this.afs.getPageParams(this);
                this.params = params;
                this.type = params.type;
                this.readOnly = params.type === "read" ? true : false;
                this.contractNo = params.contractNo;
                this.caseNo = params.caseNo;
                this.receiptId = params.receiptId;
                if (params.bizDataId === undefined) {
                    this.id = params.id;
                } else {
                    this.id = params.bizDataId;
                }
                this.Obj = {
                    isFinance: this.isFinance,
                    roleType: this.isSP,
                    caseNo: params.caseNo,
                    contractNo: params.contractNo,
                    processStatus: params.processStatus,
                    receiptId: params.receiptId
                }
            },
            getManageCollectCancel() {
                getManageCollectCancelByCaseNo(this.caseNo).then(res => {
                    if (res.code === "0000") {
                        this.pageInfo = res.data;
                        this.pageInfo.applyDate = res.data.startTime;
                        this.pageInfo.endDate = res.data.endTime;
                        this.pageInfo.applyBy = res.data.createBy;
                        this.receiptId = res.data.receiptId;
                        this.Obj.receiptId = res.data.receiptId;
                        this.$refs.business.formData=res.data;
                        this.revokeFrom=res.data;
                        this.show = true;
                    }
                });
            },

            openUploadModal() {
                if (this.Obj.caseNo || this.$refs.business.caseNo) {
                    this.fileParams = {
                        belongNo: this.Obj.contractNo,
                        busiNo: this.Obj.caseNo || this.$refs.business.caseNo,
                        busiType: 'collectionRevoke',
                        busiData: {},
                        busiNodes: 'collectionRevoke' //业务节点
                    };
                    this.isUploadFileModalOpen = true;
                } else {
                        this.formData();
                        saveCollectCancel(this.contractForm).then(res => {
                            if (res.code === "0000") {
                                this.$refs.business.formData=res.data;
                                this.revokeFrom=res.data;
                                this.isUploadFileModalOpen = true;
                            }
                        }).catch(e => {
                        });
                }
            },
            closeModal() {
                this.isUploadFileModalOpen = false;
            },
            formData() {
                this.contractForm.applyNo = this.$refs.contractInfo.formData.applyNo;
                this.contractForm.contractNo = this.$refs.contractInfo.formData.contractNo;
                this.contractForm.custName = this.$refs.contractInfo.formData.basicCustBaseInfo.custName;
                this.contractForm.startDate = this.$refs.contractInfo.formData.startDate;
                this.contractForm.contractStatus = this.$refs.contractInfo.formData.contractStatus;
                this.contractForm.channelFullName = this.$refs.contractInfo.formData.channelFullName;
                this.contractForm.channelType = this.$refs.contractInfo.formData.channelType;
                this.contractForm.contactNo = this.$refs.contractInfo.formData.basicCustBaseInfo.phone;
                this.contractForm.certNo = this.$refs.contractInfo.formData.basicCustBaseInfo.certNo;
                this.contractForm.sourceType = this.revokeFrom.sourceType;
                this.contractForm.succeedTime = this.revokeFrom.succeedTime;
                this.contractForm.arriveAmt = this.revokeFrom.arriveAmt;
                this.contractForm.sourceId = this.revokeFrom.sourceId;
                this.contractForm.receiptId = this.receiptId;
                if (this.isSP) {
                    this.contractForm.department = this.$refs.contractInfo.channelFullName;
                } else {
                    this.contractForm.department = this.userInfo.userRealName;
                }
                this.contractForm.id= this.$refs.business.formData.id;
                // this.contractForm.remarks = this.$refs.business.formData.remarks;
                if (this.$refs.contractCurrentUser !== undefined) {
                    this.contractForm.reasonType = this.$refs.contractCurrentUser.formData.reasonType;
                    this.contractForm.approvalOpinion = this.$refs.contractCurrentUser.formData.approvalOpinion;
                }
                this.contractForm.businessSource = businessSource(this.roleType);
            },
            getDictDataList() {
                getDictDataList(this.dicttArr).then(res => {
                    this.dataDict = res;
                });
            },
            back() {
                //关闭页面
                this.afs.closeTab(this);
            },
            submit() {
              this.$Modal.confirm({
                    title: "确认提交",
                    content: "您确认要提交 ?",
                    onOk: () => {
                        this.spinShow = true;
                       this.findByContractNo();
                    }
                })
            },
            checkFile() {
                if (this.Obj.caseNo || this.$refs.business.formData.caseNo) {
                    let params  = {
                        busiNo:  this.Obj.caseNo || this.$refs.business.formData.caseNo,
                        busiType: "collectionRevoke",
                    };
                    checkFile(params).then(res => {
                        if (res.code === "0000") {
                            this.fnSubmit();
                        }
                    })
                } else {
                   this.formData();
                    saveCollectCancel(this.contractForm).then(res => {
                        if (res.code === "0000") {
                            this.$refs.business.formData=res.data;
                            this.checkFile()
                        }
                    }).catch(e => {
                    });

                }

            },

            save() {
                this.formData();
                saveCollectCancel(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        this.$refs.business.formData=res.data;
                        this.revokeFrom=res.data;
                    }
                }).catch(e => {
                });
            },
            findByContractNo() {
                let params = {
                    contractBusiness: this.contractBusiness,
                    contractNo: this.contractNo,
                    handleBusiness: "collectionRevoke",
                    isSP: this.isSP
                };
                findByContractNo(params).then(res => {
                    if (res.code === "0000") {
                        if (res.msg === "success") {
                            this.fnSubmit();
                        }
                    }
                }).catch(e => {
                    this.spinShow = false;
                });
            },
            fnSubmit() {
                this.formData();
                if (this.readOnly) return;
                this.contractForm.approvalOperating = 'suggestCheckF';
                revokedStartProcess(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success("提交成功");
                        this.back();
                    }
                }).catch(e => {
                    this.spinShow = false;
                });
            },
            through() {
                this.formData();
                var content = "您确认要通过?";
                agreeValid(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        if (res.data.code != "0000") {
                            content = res.data.msg;
                            console.info(content);
                        }
                        this.$Modal.confirm({
                            title: "确认通过",
                            content: content,
                            onOk: () => {
                                this.spinShow = true;
                                this.$refs.contractCurrentUser.formValidate(
                                    "fnThrough", ""
                                );
                            }
                        })
                    }
                });

            },
            fnThrough() {
                if (this.readOnly) return;
                this.contractForm.approvalOperating = 'suggestCheckF';
                revokeSubmitProcess(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success("提交成功");
                        this.back();
                    }
                }).catch(e => {
                    this.spinShow = false;
                });
            },

            goBack() {
                this.$Modal.confirm({
                    title: "确认退回",
                    content: "您确认要退回 ?",
                    onOk: () => {
                        this.fnGoBack();
                    }
                })
            },

            refuse() {
                this.$Modal.confirm({
                    title: "确认拒绝",
                    content: "您确认要拒绝 ?",
                    onOk: () => {
                        this.spinShow = true;
                        this.fnRefuse();
                    }
                })
            },
            // 获取客户信息数据
            getCustomerInfoData(data) {
                this.customerInfo = data;
            },
            fnRefuse() {
                this.formData();
                if (this.readOnly) return;
                this.contractForm.approvalOperating = 'suggestRejectF';
                revokeSubmitProcess(this.contractForm).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success("提交成功");
                        this.back();
                    }

                }).catch(e => {
                    this.spinShow = false;
                });
            },
        }
    };
</script>
<style lang="less">
    @import url("../../../../styles/common/form.less");

    .contract-cancellation-detial-btns {
        padding-bottom: 16px;
    }

    .contract-cancellation-detial {
        .ivu-collapse-header {
            background-color: #f0faff;
        }
    }
</style>
